home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / asm / turtle.s < prev    next >
Encoding:
Text File  |  1998-10-04  |  5.6 KB  |  331 lines

  1. ;
  2. ; turtle.lib -- an ACE linked library module: turtle graphics functions.
  3. ; Copyright (C) 1998 David Benn
  4. ; This program is free software; you can redistribute it and/or
  5. ; modify it under the terms of the GNU General Public License
  6. ; as published by the Free Software Foundation; either version 2
  7. ; of the License, or (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program; if not, write to the Free Software
  16. ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. ;
  18. ; Author: David J Benn
  19. ;   Date: 3rd July 1992,
  20. ;      5th December 1992,
  21. ;      14th February 1993,
  22. ;      1st March 1993,
  23. ;      19th March 1994
  24. ;
  25.  
  26.     ; turtle graphics routines, 
  27.     ; functions & variables
  28.     xdef    _back
  29.     xdef    _forward
  30.     xdef    _heading
  31.     xdef    _home
  32.     xdef    _pendown
  33.     xdef    _penup
  34.     xdef    _setheading
  35.     xdef    _setxy
  36.     xdef    _turn
  37.     xdef    _turnleft
  38.     xdef    _turnright
  39.     xdef    _xcor
  40.     xdef    _ycor
  41.  
  42.     ; external ref's
  43.     xref    _tg_initx
  44.     xref    _tg_inity
  45.     xref    _tg_xy_ratio
  46.     xref    _radconv
  47.  
  48.     xref    _tg_degs
  49.     xref    _tg_tx
  50.     xref    _tg_ty
  51.     xref    _tg_pen
  52.     xref    _tg_distance
  53.     xref    _tg_theta
  54.     xref    _tg_temp
  55.     xref    _tg_x
  56.     xref    _tg_y
  57.     
  58.     xref    _MathBase
  59.     xref    _MathTransBase
  60.     xref    _GfxBase
  61.     xref    _RPort
  62.     xref    _LVODraw
  63.     xref    _LVOMove
  64.     xref    _LVOSPFlt
  65.     xref    _LVOSPFix
  66.     xref    _LVOSPMul
  67.     xref    _LVOSPDiv
  68.     xref    _LVOSPSin
  69.     xref    _LVOSPCos
  70.  
  71.     SECTION turtle_code,CODE
  72.  
  73. ; *** TURTLE GRAPHICS ROUTINES ***
  74.  
  75. ;
  76. ; moveturtle - primitive used by forward and back.
  77. ;         - expects distance in d0 (ffp) 
  78. ;           and direction boolean in d1 (0=back, 1=forward).
  79. ;
  80. ;         - d2 = angle (temporary store)
  81. ;
  82. _moveturtle:
  83.     ; save d0 
  84.     move.l    d0,_tg_distance
  85.  
  86.     cmpi.w    #1,d1
  87.     beq.s    _forwardcalc    
  88.     
  89.     ; reverse
  90.     move.w    _tg_degs,d2
  91.     add.w    #180,d2
  92.     ext.l    d2
  93.     divs    #360,d2
  94.     swap    d2        ; angle = (degs+180) % 360
  95.     
  96.     ext.l    d2
  97.     move.l    _MathBase,a6
  98.     move.l    d2,d0
  99.     jsr    _LVOSPFlt(a6)    ; angle = (float)angle
  100.  
  101.     move.l    _radconv,d1
  102.     jsr    _LVOSPDiv(a6)    ; theta = angle/radconv
  103.  
  104.     move.l    d0,_tg_theta
  105.  
  106.     bra.s    _calcxy        
  107.     
  108. _forwardcalc:    
  109.     ; forward    
  110.     move.l    _MathBase,a6
  111.     move.w    _tg_degs,d0
  112.     ext.l    d0
  113.     jsr    _LVOSPFlt(a6)    ; degs = (float)degs
  114.  
  115.     move.l    _radconv,d1
  116.     jsr    _LVOSPDiv(a6)    ; theta = degs/radconv
  117.     
  118.     move.l    d0,_tg_theta
  119.  
  120.     move.l    d0,d1
  121.  
  122. _calcxy:
  123.     ; calculate x
  124.     move.l    _tg_xy_ratio,d0    ; X:Y ratio = 2.25 (assumes hi-res & is approx.)
  125.     move.l    _tg_distance,d1
  126.     jsr    _LVOSPMul(a6)    
  127.     move.l    d0,_tg_temp
  128.     
  129.     move.l    _MathTransBase,a6
  130.     move.l    _tg_theta,d0
  131.     jsr    _LVOSPCos(a6)
  132.     
  133.     move.l    _MathBase,a6
  134.     move.l    _tg_temp,d1
  135.     jsr    _LVOSPMul(a6)
  136.     
  137.     jsr    _LVOSPFix(a6)    ; x = (int)(2.25*dist*cos(theta))
  138.  
  139.     move.w    d0,_tg_x
  140.  
  141.     ; calculate y
  142.     move.l    _MathTransBase,a6
  143.     move.l    _tg_theta,d0
  144.     jsr    _LVOSPSin(a6)
  145.     
  146.     move.l    _MathBase,a6
  147.     move.l    _tg_distance,d1
  148.     jsr    _LVOSPMul(a6)
  149.     
  150.     jsr    _LVOSPFix(a6)    ; y = (int)(dist*sin(theta))
  151.  
  152.     move.w    d0,_tg_y
  153.  
  154.     ; next x and y are offset from tx and ty
  155.     move.w    _tg_tx,d0
  156.     move.w    _tg_x,d1
  157.     add.w    d0,d1    
  158.     move.w    d1,_tg_x    ; x = tx+x
  159.  
  160.     move.w    _tg_ty,d0
  161.     move.w    _tg_y,d1
  162.     add.w    d0,d1    
  163.     move.w    d1,_tg_y    ; y = ty+y
  164.  
  165.     ; if pen is up -> don't draw
  166.     cmpi.b    #1,_tg_pen
  167.     beq.s    _draw_to_new_pos
  168.  
  169.     ; pen up -> don't draw, just move to x,y
  170.     move.l    _GfxBase,a6
  171.     move.l    _RPort,a1
  172.     move.w    _tg_x,d0
  173.     move.w    _tg_y,d1
  174.     jsr    _LVOMove(a6)
  175.     bra.s    _setnewtx_ty
  176.  
  177. _draw_to_new_pos:
  178.     ; pen down -> draw a line!
  179.     move.l    _GfxBase,a6
  180.     move.l    _RPort,a1
  181.     move.w    _tg_x,d0
  182.     move.w    _tg_y,d1
  183.     jsr    _LVODraw(a6)
  184.         
  185. _setnewtx_ty:
  186.     move.w    _tg_x,_tg_tx
  187.     move.w    _tg_y,_tg_ty
  188.     
  189.     rts
  190.         
  191. ;
  192. ; rotate - primitive used by setheading, turn, turnleft and turnright.
  193. ;     - expects degrees in d0 (SIGNED short integer).
  194. ;
  195. _rotate:
  196.     ext.l    d0
  197.     divs    #360,d0        ; MUST be signed division
  198.     swap    d0        ; degs = d0 % 360
  199.  
  200.         cmpi.w    #0,d0
  201.     bge.s    _storeheading
  202.     add.w    #360,d0        ; if d0 < 0 -> d0=360+d0
  203. _storeheading:
  204.     move.w    d0,_tg_degs    
  205.     rts
  206.  
  207. ;
  208. ; back - move turtle back by value in d0 (ffp).
  209. ;
  210. _back:
  211.     move.w    #0,d1
  212.     jsr    _moveturtle
  213.     rts
  214.  
  215. ;
  216. ; forward - move turtle forward by value in d0 (ffp).
  217. ;
  218. _forward:
  219.     move.w    #1,d1
  220.     jsr    _moveturtle
  221.     rts
  222.  
  223. ;
  224. ; heading - return current turtle heading in d0.
  225. ;
  226. _heading:
  227.     move.w    _tg_degs,d0
  228.     rts
  229.  
  230. ;
  231. ; home - move turtle to home position, drawing a line if pen down. 
  232. ;      - a special case of setxy.
  233. ;
  234. _home:
  235.     move.w    _tg_initx,d0
  236.     move.w    _tg_inity,d1
  237.     jsr    _setxy    
  238.     rts
  239.     
  240. ;
  241. ; pendown - put the turtle's pen down.
  242. ;
  243. _pendown:
  244.     move.b    #1,_tg_pen
  245.     rts
  246.  
  247. ;
  248. ; penup - lift the turtle's pen up.
  249. ;
  250. _penup:
  251.     move.b    #0,_tg_pen
  252.     rts
  253.  
  254. ;
  255. ; setheading - change turtle's heading to value in d0 (degrees).
  256. ;    
  257. _setheading:
  258.     jsr    _rotate
  259.     rts
  260.  
  261. ;
  262. ; setxy - change the drawing coordinates to d0,d1.
  263. ;    - expects x in d0 and y in d1.
  264. ;
  265. _setxy:
  266.     move.w    d0,_tg_tx
  267.     move.w    d1,_tg_ty
  268.  
  269.     cmpi.b    #1,_tg_pen
  270.     beq.s    _drawpath    ; if pen down -> draw!
  271.  
  272.     ; pen up -> just move to x,y
  273.     move.l    _GfxBase,a6
  274.     move.l    _RPort,a1
  275. ;    move.w    _tg_tx,d0
  276. ;    move.w    _tg_ty,d1
  277.     jsr    _LVOMove(a6)
  278.     bra    _exitsetxy
  279.  
  280. _drawpath:
  281.     ; pen down -> draw line
  282.     move.l    _GfxBase,a6
  283.     move.l    _RPort,a1
  284. ;    move.w    _tg_tx,d0
  285. ;    move.w    _tg_ty,d1
  286.     jsr    _LVODraw(a6)
  287.     
  288. _exitsetxy:    
  289.     rts
  290.  
  291. ;
  292. ; turn - rotate turtle by d0 degrees from current heading. 
  293. ;
  294. _turn:
  295.     add.w    _tg_degs,d0
  296.     jsr    _rotate
  297.     rts
  298. ;
  299. ; turnleft - rotate turtle d0 degrees to the left from current heading. 
  300. ;
  301. _turnleft:
  302.     move.w    _tg_degs,d1
  303.     sub.w    d0,d1
  304.     move.w    d1,d0
  305.     jsr    _rotate
  306.     rts
  307. ;
  308. ; turnright - rotate turtle d0 degrees to the right from current heading. 
  309. ;
  310. _turnright:
  311.     add.w    _tg_degs,d0
  312.     jsr    _rotate
  313.     rts
  314.  
  315. ;
  316. ; xcor - returns the turtle's x-coordinate in d0.
  317. ;
  318. _xcor:
  319.     move.w    _tg_tx,d0
  320.     rts
  321.  
  322. ;
  323. ; ycor - returns the turtle's y-coordinate in d0.
  324. ;
  325. _ycor:
  326.     move.w    _tg_ty,d0
  327.     rts
  328.  
  329.     END
  330.